import constUrl from '@/services/env'
import Format from '@/utils/format'
import {uploadUrl} from '@/services/model/common'
import {setLocalStorageItem, getLocalStorageItem, removeLocalStorageItem} from '@/utils/storage-helper'
import {ERP_ALL_COMPANY_SELECT_LIST, ERP_ALL_GOODS_SELECT_LIST, ERP_ALL_CUSTOMER_SELECT_LIST} from '@/utils/constant'
import {
  getGoodsSelectList,
  getCompanySelectList,
} from '@/services/ERP/goods'
import {
  getCustomerSelectList,
} from '@/services/ERP/sale'
import Permission from '@/utils/permission'
export default {
  data() {
    return {
      constUrl,
      // 文件上传流url
      uploadStreamUrl: uploadUrl,
      allGoodsSelectList: [],
      allCompanyList: [],
      allCustomerList: [],
      orderFilterString: '',
    }
  },
  computed: {
    user() {
      return this.$store.state.userInfo
    },
  },
  filters: {
    //客户重要程度
    getImportanceName(val) {
      switch (val) {
        case 1:
          return '普通'
        case 2:
          return '一般'
        case 3:
          return '关注'
        case 4:
          return '重要'
        case 5:
          return '很重要'
      }
    },
    //入库状态颜色
    gradeInOrderStatusColor(val) {
      switch (val) {
        case 1:
          return 'background-color:#0073b7'
        case 2:
          return 'background-color:#0DC26B'
        case 3:
          return 'background-color:#0DC26B'
        case 4:
          return 'background-color:#e43321'
      }
    },
    //库存状态颜色
    gradeStatusColor(val) {
      switch (val) {
        case 1:
          return 'background-color:#0DC26B'
        case 2:
          return 'background-color:#e43321'
        case 3:
          return 'background-color:#0073b7'
      }
    },
    //订单审核状态颜色
    orderStatusColor(val) {
      switch (val) {
        case 1:
          return 'background-color:#0073b7'
        case 2:
          return 'background-color:#0DC26B'
        case 3:
          return 'background-color:#e43321'
      }
    },
    //拜访状态颜色
    gradeVisitStatusColor(val) {
      switch (val) {
        case 0:
          return 'background-color:#0DC26B'
        case 1:
          return 'background-color:#0073b7'
        case 2:
          return 'background-color:#0DC26B'
        case 3:
          return 'background-color:#a1a1a1'
      }
    },
    //账单
    billingEnumColor(val) {
      switch (val) {
        case 1:
          return 'success'
        case 2:
          return ''
      }
    },
    //货品类型
    gradeGoodsEnumColor(val) {
      switch (val) {
        case 2:
          return 'danger'
        case 3:
          return 'success'
        case 4:
          return 'danger'
      }
    },
    // 时间格式化
    formatTimer(timer) {
      if (timer) {
        return Format(new Date(timer), 'yyyy-MM-dd hh:mm:ss')
      }
      return ''
    },
    // 时间格式化
    formatTimer2(timer) {
      if (timer) {
        return Format(new Date(timer), 'yyyy-MM-dd')
      }
      return '---'
    },
  },
  methods: {
    orderFilter(val) {
      this.orderFilterString = val
    },
    // 判断权限
    hasPermission(permissionStr) {
      return Permission(this.$store.state.userInfo, permissionStr)
    },
    //清空公司缓存
    removeAllCompanyStorage() {
      removeLocalStorageItem(ERP_ALL_COMPANY_SELECT_LIST)
      this.getAllCompanyList()
    },
    //清空货品缓存
    removeAllGoodsStorage() {
      removeLocalStorageItem(ERP_ALL_GOODS_SELECT_LIST)
      this.getAllGoodsSelectList()
    },
    //清空货品缓存
    removeAllCustomerStorage() {
      removeLocalStorageItem(ERP_ALL_GOODS_SELECT_LIST)
      this.getAllCustomerList()
    },
    //获取公司下拉选项
    async getAllCompanyList() {
      let company = getLocalStorageItem(ERP_ALL_COMPANY_SELECT_LIST);
      if (!company || company === null) {
        let res = await getCompanySelectList()
        company = res.data;
        //缓存7天过期
        setLocalStorageItem(ERP_ALL_COMPANY_SELECT_LIST, company, 7);
      }
      this.allCompanyList = company || []
    },
    //获取客户下拉选项
    async getAllCustomerList() {
      let customer = getLocalStorageItem(ERP_ALL_CUSTOMER_SELECT_LIST);
      if (!customer || customer === null) {
        let res = await getCustomerSelectList()
        customer = res.data;
        //缓存7天过期
        setLocalStorageItem(ERP_ALL_CUSTOMER_SELECT_LIST, customer, 7);
      }
      this.allCustomerList = customer || []
    },
    //货品组成获取货品选择列表
    async getAllGoodsSelectList() {
      let goods = getLocalStorageItem(ERP_ALL_GOODS_SELECT_LIST);
      if (!goods || goods === null) {
        let res = await getGoodsSelectList()
        goods = res.data;
        //缓存7天过期
        setLocalStorageItem(ERP_ALL_GOODS_SELECT_LIST, goods, 7);
      }
      this.allGoodsSelectList = goods || []
    },
    //处理文件下载地址
    filePathHandle(val) {
      if (val) {
        this.fileList = val.map(x => {
          return {
            url: constUrl + x.url,
            name: x.name
          }
        })
        return this.fileList
      } else {
        return []
      }
    },
  },
}
